//
//  SubmitButton.swift
//  hello
//
//  Created by hellokitty on 6/3/2021.
//  Copyright © 2021 hellokitty. All rights reserved.
//

import UIKit

class SubmitButton: UIButton {
    
    private var titleColor = Theme.textcolorLight
    private var bgColor = Theme.colorAccent
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        initialize()
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    convenience init(_ titleColor: UIColor, _ bgColor: UIColor) {
        self.init(type: .custom)
        self.titleColor = titleColor
        self.bgColor = bgColor
        
        initialize()
    }
    
    override func titleColor(for state: UIControl.State) -> UIColor? {
        switch state {
            case .disabled:
                return Theme.textcolorLight
            default:
                return titleColor
        }
    }
    
    override func backgroundImage(for state: UIControl.State) -> UIImage? {
        switch state {
            case .disabled, .highlighted:
                return UIImage.imageWithColor(Theme.colorTertiary)
            default:
                return UIImage.imageWithColor(bgColor)
        }
    }
    
    
//    override func layoutSubviews() {
//        super.layoutSubviews()
//
//    }
    
    
    func initialize() {
        //
        self.setBackgroundImage(self.backgroundImage(for: .normal), for: .normal)
        self.setBackgroundImage(self.backgroundImage(for: .disabled), for: .disabled)
        self.setBackgroundImage(self.backgroundImage(for: .highlighted), for: .highlighted)
        //
        self.setTitleColor(self.titleColor(for: .normal), for: .normal)
        self.setTitleColor(self.titleColor(for: .disabled), for: .disabled)
        self.setTitleColor(self.titleColor(for: .highlighted), for: .highlighted)
    }

}

//private lazy var confirm: SubmitButton = {
//    let button = GSubmitButton()
//    button.addTarget(self, action: #selector(confirmAction), for: .touchUpInside)
//    button.setTitle("确定".localized(), for: .normal)
//    button.titleLabel?.font = UIFont.systemFont(ofSize: 14.0, weight: .bold)
//    button.layer.cornerRadius = 22.0
//    button.layer.masksToBounds = true
//    return button
//}()

// 通过颜色生成图片
extension UIImage {
    
    class func imageWithColor(_ color: UIColor) -> UIImage{
        let rect = CGRect(x: 0, y: 0, width: 1, height: 1)
        UIGraphicsBeginImageContext(rect.size)
        let context = UIGraphicsGetCurrentContext()
        context!.setFillColor(color.cgColor)
        context!.fill(rect)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image!
    }
    
    class func imageWithColor(_ color: UIColor, frame: CGRect) -> UIImage{
//        let rect = CGRect(x: 0, y: 0, width: 1, height: 1)
        UIGraphicsBeginImageContext(frame.size)
        let context = UIGraphicsGetCurrentContext()
        context!.setFillColor(color.cgColor)
        context!.fill(frame)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image!
    }
}
